<?php
/**
 * 
 * Author: wxl
 * Date: 2016/7/6
 * Time: 13:44
 * intro:公用厨房通电
 */

class KitchenService extends BaseService
{
    /*
    *$kitchenId,$userId,$terminalId:使用模块id,用户id,终端id
    */
    public function charge($kitchenId,$userId,$terminalId,$accountId){
        $result = $this->doCharge($kitchenId,$userId,$terminalId);
        if($result['status'] == 'F'){
            return $result;
        }

        $res = $this->checkWebsite($result['websiteInfo']['website_id']);
        if($res['status'] == 'F'){
            return $res;
        }

        $result = array_merge($result,$res);

        $minMoney = M('Kitchen')->getFieldByKitchenId($kitchenId,'min_money');
        $kitchenUserInfo = $this->getKitchenUser($userId,$kitchenId);
        if(isset($result['websiteInfo']['fees']) && $result['websiteInfo']['fees']==0 && $kitchenUserInfo['overplus_time']<$minMoney){//少于所需金额
            return array('status'=>'F','message'=>'您的账户余额不足,账户金额需大于'.$minMoney.'元才能使用');
        }

        $terminalInfo = $result['terminalInfo'];
        $websiteInfo = $result['websiteInfo'];
        $degree = $this->getDegree($websiteInfo,$terminalInfo,$userId,$kitchenId);//获得电表度数

        if((isset($degree['status']) && $degree['status'] =='F')||($degree==0 && $terminalInfo['last_number'] !=0) || ($terminalInfo['last_number']>0 &&abs($degree-$terminalInfo['last_number'])>10)){//大于10为了防止抄表过来数据错误
            return array('status'=>'F','message'=>'执行失败,请重试');
        }

       $return = $this->insertCode($websiteInfo,$terminalInfo,$userId,$kitchenId,1);
        if(!$return){
            return array('status'=>'F','message'=>'使用失败');
        }else{
            //判断是否成功返回数据
            $mScheduled = D('Base/KitchenScheduledTasks');
            $taskId = $return['tasks_id'];
            $scheduledInfo = $mScheduled->getByPk($return['tasks_id'], 'fail_code,status,tasks_id');
            if($scheduledInfo['status'] == 2){//判断是否操作指令执行失败
                
                if($scheduledInfo['fail_code'] == 1002 || $scheduledInfo['fail_code'] == 1003){
                    return array('status'=>'F','message'=>'设备网络连接异常，请联系管理员');
                }else{
                     return array('status'=>'F','message'=>'使用失败， 请稍后再尝试');
                }
            }elseif($scheduledInfo['status'] == 0){//未执行完指令，进行等待
                
                for($i=0;$i<=4;$i++){
                    //检测状态，2秒一次，执行6次
                    sleep(2);
                    $return = $this->check_tasks($scheduledInfo['tasks_id']);//继续查询
                    
                    if($return == 2){
                        break;
                    }

                }
                
                if($return == 1){//使用指令未成功 
                    return array('status'=>'F','message'=>'使用失败，请稍后再尝试');
                }

                if(isset($return['status'])){
                    return $return;
                }
            }
        }

        if($degree == 0){
            $degree = $terminalInfo['last_number'];
        }
        //$degree = $terminalInfo['last_number'];
        
        $array = array(
                'kitchen_id' => $kitchenId,
                'website_id' => $websiteInfo['website_id'],
                'user_id' => $userId,
                'status' => 0,
                'type' => 1,
                'start_time' => time(),
                'terminal_id' => $terminalInfo['terminal_id'],
                'terminal_code' => $terminalInfo['terminal_code'],
                'freezing_time' => $minMoney,
                'create_time' => time(),
                'start_degree'=>$degree
        );

        $mLog = D('Base/KitchenLog');
        $mWechatUser = D('Wechat/WechatUser');
        $returnLog = $mLog->update($array);//更新使用记录表
        
        $mKitchenUser = D('KitchenUser');
        $updateUser = array(
                            'id'=>$kitchenUserInfo['id'],
                            'overplus_time'=>$kitchenUserInfo['overplus_time']-$minMoney
                            );
        $mKitchenUser->update($updateUser);//先扣除金额
        if($returnLog !== false){
            //更新终端为合闸
            $mTerminal = D('KitchenTerminal');
            $mTerminal->update(array('terminal_id'=>$terminalId,'type'=>1,'last_number'=>$degree));
                
            $WechatUserInfo = $mWechatUser->getByUserId($userId);
            if($WechatUserInfo['subscribe']){
                //给用户推消息
                import("@.Msgtpl.Base.KitchenMsgtpl");
                $chargingMsgtpl = new KitchenMsgtpl($WechatUserInfo['wechat_id']);
                $chargingMsgtpl->sendMessageToUser($WechatUserInfo,$kitchenId,$value,$returnLog['kitchen_log_id']);

            }
            return array('status'=>'S','message'=>'操作成功','id'=>$returnLog['kitchen_log_id']);
        }else{
            return array('status'=>'F','message'=>'操作失败');
        }

    }


    //获取电表返回值（新）
    public function check_tasks($tasks_id){
        
        $mScheduled = D('Base/KitchenScheduledTasks');
        $scheduledInfo = $mScheduled->getByPk($tasks_id , 'fail_code,status,tasks_id');//获取任务表记录
        
        if($scheduledInfo['status'] == 0){//执行指令未成功 
            return 1;
        }elseif($scheduledInfo['status'] == 2){//执行指令失败
            
            if($scheduledInfo['fail_code']==1002 || $scheduledInfo['fail_code'] == 1003){
                return array('status'=>'F','message'=>'设备网络连接异常，请联系管理员');
            }else{
                return array('status'=>'F','message'=>'使用失败，请稍后再尝试');
            }
        }else{
            return 2;
        }
    }

    //使用检测
    public function doCharge($kitchenId,$userId,$terminalId){
  
        $mLog =  D('Base/KitchenLog');
        //该设备是否有人占用
        $condition = array(
                'status' => 0,
                'terminal_id' => $terminalId,
                'user_id' => array('neq', $userId),
                'type' => 1
        );

        $num = $mLog->getCount($condition);

        if($num>0){
            return array('status'=>'F','message'=>'该设备有人在使用');
        }

        $condition = array(
                'kitchen_id' => $kitchenId,
                'status' => 0,
                'user_id' => $userId,
                'type' => 1
        );
        $kitchenLogInfo = $mLog->getInfo($condition);

        if($kitchenLogInfo){
            return array('status'=>'F','message'=>'您使用还没结束，请勿重复提交','url'=>U('/Base/kitchen/charging',array('kitchenId'=>$kitchenId,'id'=>$kitchenLogInfo['kitchen_log_id'])));
        }

        //使用条件判断
        $result = $this->allowCharge($kitchenId,$userId,$terminalId);
        if($result['status'] == 'F'){
            return $result;
        }
        //获得使用时长配置
        $result['kitchenExchange'] = $this->getKitchenExchange($kitchenId);

        return $result;
    }

    //使用条件判断
    public function allowCharge($kitchenId,$userId,$terminalId){
        //检测终端
        $result = $this->checkTerminal($terminalId);
        if($result['status'] == 'F'){
            return $result;
        }
        $fees = $result['websiteInfo']['fees'];
        //检测余额
        $res = $this->checkTime($userId,$kitchenId,$fees);
        if($res['status'] == 'F'){
            return $res;
        }

        return array_merge($result,$res);
    }

    //获得终端信息
    public function getTerminal($terminalId){
        $mTerminal = D('Base/KitchenTerminal');
        return $mTerminal->getByPk($terminalId, 'terminal_id,website_id,status,terminal_code,line_num,type,meter_type,last_number');
    }
    

    //获得网点信息
    public function getWebsite($websiteId){
        $mWebsite = D('Base/KitchenWebsite');
        return $mWebsite->getByPk($websiteId);
    }
 

    //判断某网点的状态
    public function checkWebsite($websiteId){
        $websiteInfo = $this->getWebsite($websiteId);
        if($websiteInfo && $websiteInfo['status'] == 1){
          return  array('status'=>'F','message'=>'该网点使用临时关闭中');
        }
        $currentTime=date('H')*3600+date('i')*60;
        
        /*if($websiteInfo['start_time'] > $websiteInfo['end_time']){//设置成过夜的时间限制开关
            $if = ($currentTime > $websiteInfo['start_time'] || $currentTime < $websiteInfo['end_time']);
        }else{
            $if = ($currentTime > $websiteInfo['start_time'] && $currentTime < $websiteInfo['end_time']);
        }
        
        if($websiteInfo['timeLimit'] == 1 && $if){//判断是否使用保在关闭时间段
            $currentTimeStart=date('H:i', $websiteInfo['start_time']-28800);
            $currentTimeEnd=date('H:i', $websiteInfo['end_time']-28800);
            $this->ajaxReturn(array('status'=>'F','message'=>"为了避免影响同学休息，{$currentTimeStart}-{$currentTimeEnd}使用功能不开放，所有使用端口均进行断电，请同学们合理安排使用时间。"));
        }*/
        //判断是否在关闭时间内
        return array('status'=>'S','websiteInfo'=>$websiteInfo);
    }


    //判断终端信息是否存在
    public function checkTerminal($terminalId){
        $terminalInfo = $this->getTerminal($terminalId);
        if(!$terminalInfo){
            return array('status'=>'F','message'=>'参数错误');
        }

        if($terminalInfo['status'] == 1){
            return array('status'=>'F','message'=>'该终端临时关闭中'); 
        }
        $websiteInfo = $this->getWebsite($terminalInfo['website_id']);
        return array('status'=>'S','terminalInfo'=>$terminalInfo,'websiteInfo'=>$websiteInfo);
    }

    /**
     * 获得使用用户信息
     * @param $userId
     * @param $kitchenId
     * @return mixed
     */
    public function getKitchenUser($userId,$kitchenId){
        $mUser = D('Base/KitchenUser');
        return $mUser->getInfo(array('user_id'=>$userId));
    }
    
    //判断用户信息是否正确、金额是否充足
    /*
    * fees:0：收费，1：免费
    */
    public function checkTime($userId,$kitchenId,$fees = 0){

        $kitchenUserInfo = $this->getKitchenUser($userId,$kitchenId);
        if(!$kitchenUserInfo){
            return array('status'=>'F','message'=>'帐号信息不正确');
        }

        if($kitchenUserInfo['overplus_time'] ==0 && $fees==0){
            if(IS_AJAX){
                $msg = '剩余金额不足';
            }else{
                $msg = '剩余金额不足,<a href="'.C('site_url').'/index.php?g=Base&m=KitchenReCharging&a=chargingRecharge&kitchenId='.$kitchenId.'">前往充值</a>';
            }
            return array('status'=>'F','message'=>$msg);
        }
        $remainTime = $this->timeChangeOver($kitchenUserInfo['overplus_time']);
        return array('status'=>'S','kitchenUserInfo'=>$kitchenUserInfo,'remainTime'=>$remainTime);
    }
    

    //获得使用时长配置
    public function getKitchenExchange($kitchenId){
        $kitchenExchange = D('Base/KitchenExchange');
        $condition = array(
                'kitchen_id'=>$kitchenId
            );
        return $kitchenExchange->getList($condition,'*','value asc');
    }


    public function getKitchen($kitchenId){
        $mKitchen = D('Base/Kitchen');
        return $mKitchen->getByKitchenId($kitchenId);
    }

    /**
     * 时间转换
     * @param int $time
     */
    public function timeChangeOver($time){
        $hour = (int) ($time / 60);
        $minute = $time % 60;
        if($hour){
            $timeChange = $hour.'小时';
        }
        if($minute){
            $timeChange .= $minute.'分钟';
        }
        return $timeChange?$timeChange:0;
    }

    /*
    * @param:$type 1:合闸  2拉闸,3.检测电流
    */
    function checkMeterType($terminalInfo,$type=1,$password = 0){
        //电表类型判断
        if(!$terminalInfo['meter_type']){//判断电表类型是否正确 
            return array('status'=>'F','message'=>'电表类型错误');
        }
        if($type == 1){//合闸
            $str = 'getMakeAmmeterOnCode';
            $successStr = 'getMakeAmmeterOnSuccessCode';
        }else if($type == 2){//拉闸
            $str = 'getMakeAmmeterOffCode';
            $successStr = 'getMakeAmmeterOffSuccessCode';
        }else if($type == 3){//抄电表度数
            $str = 'getReadKWHCode';
        }
        vendor('Kitchen.'.$terminalInfo['meter_type']);
  
        if($type == 3){
            $operate_code = call_user_func(array($terminalInfo['meter_type'],$str),$terminalInfo['terminal_code']);
        }else{
            $operate_code = call_user_func(array($terminalInfo['meter_type'],$str),$terminalInfo['terminal_code'],$password);//获取电表拉合闸码
        }
        

        if($type == 3){
            $out_success = '';
        }else{
           $out_success = call_user_func(array($terminalInfo['meter_type'],$successStr),$terminalInfo['terminal_code']);//获取电表成功返回码 
        }
        $out_success = strtolower(str_replace(' ','',$out_success));

        return array('status'=>'S','operate_code'=>$operate_code,'out_success'=>$out_success);
    }



    /*
    * 结束使用
    */
    public function endCharge($kitchenLogId,$kitchenId,$userId){
        $kitchenUserInfo = $this->getKitchenUser($userId,$kitchenId);
        if(!$kitchenUserInfo){
            return array('status'=>'F','message'=>'用户信息不存在');
        }

        //获得使用信息
        $mLog = D('Base/KitchenLog');
        $logInfo = $mLog->getByPk($kitchenLogId);
        if(!$logInfo || $logInfo['status'] == 1){
            return array('status'=>'F','message'=>'您没有使用任务，无法结束使用');
        }

       
        //获得网点、终端信息
        $websiteInfo = $this->getWebsite($logInfo['website_id']);
        $terminalInfo = $this->getTerminal($logInfo['terminal_id']);

        
        
        //获得电表度数
       
        $degree = $this->getDegree($websiteInfo,$terminalInfo,$userId,$kitchenId);//获得电表度数
        if((isset($degree['status']) && $degree['status'] =='F')||($degree==0 && $terminalInfo['last_number'] !=0)|| ($terminalInfo['last_number']>0 && abs($degree-$terminalInfo['last_number'])>10)){
            return array('status'=>'F','message'=>'执行失败,请重试');
        }

        $kitchenInfo = $this->getKitchen($kitchenId);
        $consumeMoney = ($degree-$logInfo['start_degree'])/$kitchenInfo['ratio'];//换算成金额

        //结束扣费
        if ($websiteInfo['fees'] ==1) //如果终端所在网点是免费的
        {
            $surplusTime = $kitchenUserInfo['overplus_time'];
        }else {
            $surplusTime = $kitchenUserInfo['overplus_time']+$logInfo['freezing_time']-$consumeMoney;//加上开始扣除的冻结金额
        }
        $endTime = time();
        $array = array(
                'kitchen_log_id' => $kitchenLogId,
                'end_time' => $endTime,
                'status' => 1,
                'consume_time' => $consumeMoney,//消费金额
                'surplus_time' => $surplusTime,//剩余金额
                'end_degree'   => $degree               
        );

        $logInfo = $mLog->getByPk($kitchenLogId);//重新查询一次是否已经被结束

        if($logInfo['status'] == 0){
            $mUser = D('Base/KitchenUser');
            $returnLog = $mLog->update($array);//结束更新表
            if($returnLog){
                $where = array(
                        'id' => $kitchenUserInfo['id'],
                        'overplus_time' => $array['surplus_time'],
                );
                $returnUser = $mUser->update($where);//更新用户表

                $returnScheduled = $this->insertCode($websiteInfo,$terminalInfo,$userId,$kitchenId,2);
                if(!$returnScheduled){
                    $this->ajaxReturn(array('status'=>'F','message'=>'拉闸失败'));
                }else{
                    //终端状态更新为拉闸
                    $mTerminal = D('KitchenTerminal');
                    $mTerminal->update(array('terminal_id'=>$logInfo['terminal_id'],'type'=>2,'last_number'=>$degree));

                    $mWechatUser = D('Wechat/WechatUser');
                    $WechatUserInfo = $mWechatUser->getByUserId($userId);
                    if($WechatUserInfo['subscribe']){
                        //给用户推消息
                        import("@.Msgtpl.Base.KitchenMsgtpl");
                        $chargingMsgtpl = new KitchenMsgtpl($WechatUserInfo['wechat_id']);
                        $chargingMsgtpl->sendMessageToUser2($WechatUserInfo,$logInfo,$kitchenId);
                    }
                }
                return array('status'=>'S','message'=>'操作成功');
            }
        }else{
            return array('status'=>'S','message'=>'已操作成功');
        }

    }

     //插入拉闸的指令  type:1合闸，2：拉闸
    public function insertCode($websiteInfo,$terminalInfo,$userId,$kitchenId,$type = 1){
            
            //获得指令
            $meterArr = $this->checkMeterType($terminalInfo,$type,$websiteInfo['hub_device_password']);


            $kitchenInfo = $this->getKitchen($kitchenId);

            $condition = array(
                        'website_id' => $websiteInfo['website_id'],
                        'terminal_id' => $terminalInfo['terminal_id'],
                        'terminal_code' => $terminalInfo['terminal_code'],
                        'operate_code' => $meterArr['operate_code'],
                        'ip' => $websiteInfo['hub_socket_ip'],
                        'port' => $websiteInfo['hub_socket_port'],
                        'type' => $type,
                        'address_id' => $websiteInfo['hub_device_id'],
                        'out_success' => $meterArr['out_success'],
                        'notify_url' => '',
                        'create_time' => time(),
                        'user_id' => $userId,
                        'account_id' => $kitchenInfo['account_id'],
            );
                $mScheduled = D('Base/KitchenScheduledTasks');
                $returnScheduled = $mScheduled->update($condition);//更新任务表
                
               return $returnScheduled;
    }


    /*检测使用状态*/
    public function checkCharge($kitchenLogId){
        $mLog = D('Base/KitchenLog');
        $logInfo = $mLog->getByPk($kitchenLogId , 'status,kitchen_log_id');
        if($logInfo !== false && $logInfo['status'] == 1){
            return array('status'=>'S','message'=>'使用已结束');
        }else{
            return array('status'=>'F','message'=>'正在使用中');
        }
    }



    /*定时任务*/
    public function timedTask(){
        set_time_limit(0);
        $mWechatUser = D('Wechat/WechatUser');
        $mKitchen = D('Base/Kitchen');
        $mLog = D('Base/KitchenLog');
        $mWebsite = D('Base/KitchenWebsite');
        $mTerminal = D('Base/KitchenTerminal');
        $mScheduled = D('Base/KitchenScheduledTasks');
        $mUser = D('Base/KitchenUser');
        
        $condition = array(
                'status'=>0,
                'type' => 1
        );
        $field = 'kitchen_log_id,kitchen_id,website_id,user_id,terminal_id,terminal_code,start_time,freezing_time,start_degree,end_degree';
        $logList = $mLog->getList($condition,$field,'kitchen_log_id');//正在使用记录的列表
        foreach ($logList as $key => $value){
            /*if($value['terminal_id'] != 58){//测试用
                continue;
            }*/
            $kitchenInfo = $mKitchen->getByKitchenId($value['kitchen_id']);//获取账号
            $accountId = $kitchenInfo['account_id'];
            $condition = array(
                    'user_id' => $value['user_id'],
                    'kitchen_id' => $value['kitchen_id'],
            );
            $userInfo = $mUser->getInfo($condition , 'user_id,id,overplus_time');//用户信息
            
            if(!$userInfo){
                continue;
            }
            $terminalInfo = $mTerminal->getByPk($value['terminal_id']);//终端信息
            $websiteInfo = $mWebsite->getByPk($value['website_id']);//网点信息
            if($websiteInfo['fees'] == 1){
                continue;
            }

            //后台没配则跳过不执行
            if(!$kitchenInfo['test_time'] || !$kitchenInfo['over_degree']){
                continue;
            }
            //抄表次数，不包含开始使用那次
            $degreeNums = M('KitchenDegree')->where(array('kitchen_log_id'=>$value['kitchen_log_id']))->count();
            if((time()-$value['start_time'])/($kitchenInfo['test_time']*60)<$degreeNums+1){//不到间隔时间
                continue;
            }

            $degree = $this->getDegree($websiteInfo,$terminalInfo,$value['user_id'],$value['kitchen_id']);
            if((isset($degree['status']) && $degree['status'] =='F')||($degree==0 && $terminalInfo['last_number'] !=0) || ($terminalInfo['last_number']>0 &&abs($degree-$terminalInfo['last_number'])>10)){
                continue;
            }else{
                //获得最近一次间隔时间前抄表度数
                $degreeInfo = M('KitchenDegree')->where(array('kitchen_log_id'=>$value['kitchen_log_id'],'create_time'=>array('elt',time()-$kitchenInfo['test_time']*60)))->order('degree_id desc')->find();
                
                //插入当次抄表数据
                $degreeData = array(
                        'kitchen_id'    =>  $value['kitchen_id'],
                        'kitchen_log_id'=>  $value['kitchen_log_id'],
                        'degree'        =>  $degree,
                        'terminal_code' =>  $value['terminal_code'],
                        'create_time'   =>  time()
                        );
                M('KitchenDegree')->update($degreeData);

                if($kitchenInfo['ratio']){
                    $consumeMoney = ($degree-$value['start_degree'])/$kitchenInfo['ratio'];
                }else{
                    $consumeMoney = $degree-$value['start_degree'];
                }
               

                $surplus_time = $userInfo['overplus_time']+$value['freezing_time']-$consumeMoney;
                //余额不足了，自动结束掉
                if($surplus_time<=0){
                    $surplus_time = 0;
                }else{
                   if(!$degreeInfo){
                    //说明是第一个间隔时间，不给结束
                    continue;
                    }else{

                        if($degree-$degreeInfo['degree']>$kitchenInfo['over_degree']){
                            //度数花费多说明一直在用
                            continue;
                        }
                        //余额充足，但相隔时间内度数没多少变化，可能是忘记手机端结束，自动给他结束
                    } 
                }

                
                //不在用，用户没结束掉的，检测到自动给他结束

                
                
                /*if($consumeMoney >= $value['freezing_time']){
                    $consumeMoney = $value['freezing_time'];*/
                    $array = array(
                            'kitchen_log_id' => $value['kitchen_log_id'],
                            'end_time' => time(),
                            'status' => 1,
                            'consume_time' => $consumeMoney,//消费金额
                            'surplus_time' => $surplus_time,//$userInfo['overplus_time']-$consume_time,
                            'end_degree'   => $degree 
                                
                    );
                    $logInfo = $mLog->getByPk($value['kitchen_log_id'],'status');//重新查询一次是否已经被结束
                    
                    if($logInfo['status'] == 0){
                        $returnLog = $mLog->update($array);//结束更新表
                        if($returnLog){
                            $where = array(
                                    'id' => $userInfo['id'],
                                    'overplus_time' => $array['surplus_time'],
                            );
                            $returnUser = $mUser->update($where);//更新用户表
                           

                            $returnScheduled = $this->insertCode($websiteInfo,$terminalInfo,$value['user_id'],$value['kitchen_id'],2);

                            $mTerminal->update(array('terminal_id'=>$value['terminal_id'],'type'=>2,'last_number'=>$degree));//终端状态修改为拉闸
                            if(!$returnScheduled){
                                break;
                            }else{
                                $WechatUserInfo = $mWechatUser->getByUserId($value['user_id']);
                                if($WechatUserInfo['subscribe']){                           

                                    //模板空间
                                    //给用户推消息
                                    import("@.Msgtpl.Base.KitchenMsgtpl");
                                    $chargingMsgtpl = new KitchenMsgtpl($WechatUserInfo['wechat_id']);
                                    $chargingMsgtpl->sendMessageToUser2($WechatUserInfo,$logInfo,$value['kitchen_id']);
                                }
                            }
                        }
                    }
                // }
            }


            
            
        }
    }


    //抄电表度数
    public function getDegree($websiteInfo,$terminalInfo,$userId,$kitchenId){
        vendor('Kitchen.'.$terminalInfo['meter_type']);
 
        $result = $this->insertCode($websiteInfo,$terminalInfo,$userId,$kitchenId,$type = 3);

        //判断是否成功返回数据
            $mScheduled = D('Base/KitchenScheduledTasks');
            $taskId = $result['tasks_id'];
            $scheduledInfo = $mScheduled->getByPk($taskId, 'fail_code,status,tasks_id');
            if($scheduledInfo['status'] == 2){//判断是否操作指令执行失败
                
                if($scheduledInfo['fail_code'] == 1002 || $scheduledInfo['fail_code'] == 1003){
                    return array('status'=>'F','message'=>'设备网络连接异常，请联系管理员');
                }else{
                     return array('status'=>'F','message'=>'使用失败， 请稍后再尝试');
                }
            }elseif($scheduledInfo['status'] == 0){//未执行完指令，进行等待
                
                for($i=0;$i<=4;$i++){
                    //检测状态，2秒一次，执行6次
                    sleep(2);
                    $return = $this->check_tasks($scheduledInfo['tasks_id']);//继续查询
                    
                    if($return == 2){
                        break;
                    }

                }
                
                if($return == 1){//使用指令未成功 
                    return array('status'=>'F','message'=>'抄电失败');
                }

                if(isset($return['status'])){
                    return $return;
                }
            }
        $scheduledInfo = $mScheduled->getByPk($taskId, 'return_code');  
        if($scheduledInfo['return_code']){
            $degree = call_user_func(array($terminalInfo['meter_type'],'getDumpEnergyFromCode'),$scheduledInfo['return_code']);//获取度数

            return $degree;
        }else{
            return 0;
        }
        

    }

}